package org.UserHouse.Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.UserHouse.Dao.HitemDao;
import org.UserHouse.Dao.Imp.HitemDaoImp;
import org.UserHouse.entity.Hitem;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author bin
 * @date 2024-12-24
 */
@WebServlet("/update.Hitem.do")
public class UpdateHitemServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        int hid = Integer.parseInt(req.getParameter("Hid"));
        String hname = req.getParameter("Hname");
        int tid = Integer.parseInt(req.getParameter("Tid"));
        Double hprice = Double.parseDouble(req.getParameter("Hprice"));
        String hgrade = req.getParameter("Hgrade");
        String hcontent = req.getParameter("Hcontent");
        String hdate = req.getParameter("Hdate");
        String himg = req.getParameter("Himg");

        Hitem hitem = new Hitem();
        hitem.setHid(hid);
        hitem.setHname(hname);
        hitem.setTid(tid);
        hitem.setTprice(hprice);
        hitem.setHgrade(hgrade);
        hitem.setHcontent(hcontent);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(hdate);
            hitem.setHdate(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        hitem.setHimg(himg);

        HitemDao hitemDao = new HitemDaoImp();
        int n = hitemDao.UpdateHitem(hitem);
        out.println(n);

        out.flush();
        out.close();
    }
}
